package mytime

import (
	"fmt"
	"time"
	"strings"
	"strconv"
)

type mytime struct {
	h int
	m int
	s int
	d int
}
//字符获取今天时间戳
func TodayUnix(mytime string)  int64{
	loc, _ := time.LoadLocation("Local")
	//fmt.Println(loc.String())
	timeStr := time.Now().Format("2006-01-02")
	//fmt.Println("timeStr:", timeStr)
	t, _ := time.ParseInLocation("2006-01-02", timeStr,loc)
	//timeNumber := t.Unix()
	fmt.Println("timeNumber:", t.Unix())
	return t.Unix()
}
//字符获取今天时间戳2
func TodayUnix2(mytime string)  int64{
	mt,err:=strSplitTime(mytime)
	if err!=nil {
		return 0
	}
	t := time.Now()
	tm1 := time.Date(t.Year(), t.Month(), t.Day(), mt.h, mt.m, mt.s, mt.d, t.Location())
	return tm1.Unix()
}
//字符获取明天时间戳
func TomUnix(mytime string)  int64{
	mt,err:=strSplitTime(mytime)
	if err!=nil {
		return 0
	}
	t := time.Now()
	tm1 := time.Date(t.Year(), t.Month(), t.Day()+1, mt.h, mt.m, mt.s, mt.d, t.Location())
	return tm1.Unix()
}
//根据字符串如果该字符串的时间已过返回明天的
func AutoUseUnix(mytime string) int64  {
	mt,err:=strSplitTime(mytime)
	if err!=nil {
		return 0
	}
	t := time.Now()
	tm1 := time.Date(t.Year(), t.Month(), t.Day(), mt.h, mt.m, mt.s, mt.d, t.Location())
	if tm1.Before(time.Now()) {
		tm1=time.Date(t.Year(), t.Month(), t.Day()+1, mt.h, mt.m, mt.s, mt.d, t.Location())
	}
	return tm1.Unix()
}
func AutoUseUnixInt(mytime string) int {
	return int(AutoUseUnix(mytime))
}
func TomUnixInt(mytime string)  int{
	return int(TomUnix(mytime))
}
//分割时间字符串
func strSplitTime(myt string) (mytime,error) {
	//t := time.Now()
	//tm1 := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
	//tm2 := tm1.AddDate(0, 0, 1)
	st:=strings.Split(myt,":")
	cd:=len(st)
	var stcd [4]int
	if  cd>0{
		stcd[0],_=strconv.Atoi(st[0])
	}
	if  cd>1{
		stcd[1],_=strconv.Atoi(st[1])
	}
	if  cd>2{
		stcd[2],_=strconv.Atoi(st[2])
	}
	if  cd>3{
		stcd[3],_=strconv.Atoi(st[3])
	}
	return mytime{h:stcd[0],m:stcd[1],s:stcd[2],d:stcd[3]},nil
}
//该时段是否介于xx-xx间
func BetweenTimeNow(star string ,end string)  bool{
	star_t:=TodayUnix2(star)
	end_t:=TodayUnix2(end)
	if star_t<end_t {
		return time.Now().Unix()>star_t&&time.Now().Unix()<=end_t
	}else {
		return !(time.Now().Unix()>end_t&&time.Now().Unix()<=star_t)
	}
	return false

}
//自定义时间是否介于xx-xx间
func BetweenTime(star string ,end string,myt int64)  bool{
	star_t:=TodayUnix2(star)
	end_t:=TodayUnix2(end)
	if star_t<end_t {
		return myt>star_t&&myt<=end_t
	}else {
		return !(time.Now().Unix()>end_t&&time.Now().Unix()<=star_t)
	}
	return false
}
func NowUnixInt()int  {
	return int(time.Now().Unix())

}
//某时间是否大于现在时间
func BiggerNowTime(t time.Time)  bool{
	return t.Unix()>time.Now().Unix()
}
//某时间是否小于现在时间
func SmallerNowTime(t time.Time)  bool{
	return t.Unix()<time.Now().Unix()
}
//某时间与现在时间距离都烧苗
func DistanceNowTime(t time.Time)  int64{
	return t.Unix()-time.Now().Unix()
}
func DistanceNowTime2(t int)  int{
	return t-int(time.Now().Unix())
}
//真心转time
func Int2Time(t int)  time.Time{
	return time.Unix(int64(t),0)
}